<?php

$this->LoadHelper('MemberHelper');
$MemberHelper = new MemberHelper();

$this->LoadHelper('ApiHelper');
$ApiHelper = new ApiHelper();

$this->LoadHelper('SmsHelper');
$SmsHelper = new SmsHelper();

$option = empty($this->Param['option']) ? 'login' : $this->Param['option'];
switch ($this->Param['option']) {
    case 'login':
        $data['u_name'] = empty($this->Param['u_name']) ? ErrorMsg::ApiMsg('请输入用户名') : $this->Param['u_name'];
        $data['u_password'] = empty($this->Param['u_password']) ? ErrorMsg::ApiMsg('请输入密码') : $this->Param['u_password'];
        (!preg_match('/[0-9]/', $data['u_password'])||!preg_match('/[a-zA-Z]/', $data['u_password']) || strlen($data['u_password'])<6) ? ErrorMsg::ApiMsg('您的密码过于简单，请通过您预留的手机号码重新设置新的密码。敬请谅解！') : $this->Param['u_password'];
        $MemberHelper = new MemberHelper();
        $status = $MemberHelper->doLogin($data);
        if ($status['success']) {
            $token = $ApiHelper->apiSave($status['data']['u_id']);
            $member_info = $MemberHelper->getMember($status['data']['u_id']);
            if($member_info['u_dp_type'] == 'dot' && $member_info['u_scope'] == '部门级'){
                $branch_manager = 1;
            }else{
                $branch_manager = 0;
            }
            if($member_info['u_dp_type'] != 'dot'){
                ErrorMsg::ApiMsg('非网点人员不允许登录');
            }
            $data = array(
                'u_id' => $status['data']['u_id'],
                'u_realname' => $status['data']['u_realname'],
                'u_mobile' => $status['data']['u_mobile'],
                'u_token' => $token['tk_token'],
                'u_token_exp' => $token['tk_expiration'],
                'u_dpname' => $member_info['u_dpname'],
                'u_role_id' => $member_info['role_id'],
                'u_role_name' => $member_info['role_name'],
                'u_brmanager' => $branch_manager
            );
            unset($this->Param['u_password']);
            $msg = '登录成功';
            $act_log_name = 'login';
            if(in_array($member_info['u_role'], array(5, 6))){}else{
//                ErrorMsg::ApiMsg('非业务员或网点负责人不允许登录');
                $msg = '登录成功，你的角色为内勤，如有必要请在2018-05-21之前联系颜倩';
                $act_log_name = 'login_'.$member_info['u_role'];
            }
            $ApiHelper->logSave($act_log_name, $this->Param);
            ErrorMsg::ApiMsg($msg, 0, $data);
        } else {
            ErrorMsg::ApiMsg($status['msg']);
        }
        break;
    case 'getpwd':
        $u_name = empty($this->Param['u_name']) ? ErrorMsg::ApiMsg('请输入手机号') : $this->Param['u_name'];
        $u_repassword = empty($this->Param['u_repassword']) ? ErrorMsg::ApiMsg('请输入密码') : $this->Param['u_repassword'];
        (!preg_match('/[0-9]/', $u_repassword)||!preg_match('/[a-zA-Z]/', $u_repassword) || strlen($u_repassword)<6) ? ErrorMsg::ApiMsg('请设置至少包含数字以及字母并长度6位及以上的密码。敬请谅解！') : $this->Param['u_password'];
        $code = empty($this->Param['u_code']) ? ErrorMsg::ApiMsg('请输入验证码') : $this->Param['u_code'];
        $sms_verify = $SmsHelper->smsVerify($u_name, $code);
        if($sms_verify){
            $member_info = $MemberHelper->getMemberRow(array(
                '`u_mobile` = ?' => $u_name,
                '`u_status` = ?' => 'ok'
            ));
            if(empty($member_info)){
                ErrorMsg::ApiMsg('无当前用户信息');
            }else{
                $data = array();
                $data['u_salt'] = StringCode::RandomCode(6);
                $data['u_password'] = md5(md5($u_repassword).$data['u_salt']);
                $uid = $member_info['u_id'];
                unset($member_info['u_id']);
                $MemberHelper->memberUpdate($data, $uid);
                $ApiHelper->logSave('resetpwd', $this->Param);
                ErrorMsg::ApiMsg('密码重置成功', 0);
            }
        }else{
            ErrorMsg::ApiMsg('验证码校验失败');
        }
        break;
    default :
}
